package com.atguigu.syt.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.syt.hosp.repository.DepartmentRepository;
import com.atguigu.syt.hosp.service.DepartmentService;
import com.atguigu.syt.model.hosp.Department;
import com.atguigu.syt.vo.hosp.DepartmentVo;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentRepository departmentRepository;

    //保存科室信息
    @Override
    public void save(Map<String, Object> paramMap) {
        Department department = JSONObject.
                parseObject(JSONObject.toJSONString(paramMap), Department.class);
        Department byHoscodeAndDepcode = departmentRepository.findByHoscodeAndDepcode(
                department.getHoscode(), department.getDepcode());
        if (byHoscodeAndDepcode != null) {
            department.setId(byHoscodeAndDepcode.getId());
            departmentRepository.save(department);
        } else {
            departmentRepository.save(department);
        }
    }

    /**
     * 分页查询
     *
     * @param page    当前页码
     * @param limit   每页记录数
     * @param hoscode 查询条件
     * @return
     */

    @Override
    public Page<Department> selectPage(int page, int limit, String hoscode) {

        Pageable Pageable = PageRequest.of(page - 1, limit, Sort.by(Sort.Direction.ASC, "depcode"));

        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        Page<Department> pages = departmentRepository.findAll(example, Pageable);
        return pages;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    /**
     * 根据医院编码获取部门嵌套列表
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();
        //根据医院编号，查询医院所有科室信息
        List<Department> departmentList = departmentRepository.findByHoscode(hoscode);
        //根据大科室编号  bigcode 分组，获取每个大科室里面下级子科室
        //collect：将流转换为其他形式。接收一个 Collector接口的实现，用于给Stream中元素做汇总的方法
        Map<String, List<Department>> deparmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合 deparmentMap   （iter）
        for (Map.Entry<String, List<Department>> entry : deparmentMap.entrySet()) {
            //大科室编号
            String key = entry.getKey();
            //大科室编号对应的全部数据
            List<Department> subList = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(key);
            departmentVo.setDepname(subList.get(0).getDepname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department subDepartment : subList) {
                DepartmentVo subDepartmentVo = new DepartmentVo();
                subDepartmentVo.setDepcode(subDepartment.getDepcode());
                subDepartmentVo.setDepname(subDepartment.getDepname());
                //封装到list集合
                children.add(subDepartmentVo);
            }
            //把小科室list集合放到大科室children里面
            departmentVo.setChildren(children);
            //放到最终result里面
            result.add(departmentVo);
        }
       return result;
    }
}
